function checkCustomerNo() {
    toastr.options = messageOpts;
    //校验客户号是否输入
    if ($("#customerNo").val() === "" || $("#customerNo").val().length != 6) {
        $('#customerNo').val("");
        $('#customerName').val("");
        toastr.warning('请输入有效的6位Custemr Number!');
        return;
    }
    //对客户号进行验证是否存在
    $.ajax({
        url: window.portAddress + "/customer/customerList",
        type: "get",
        data: {
            customerNo: $('#customerNo').val(),
        },
        dataType: "json",
        success: function (res) {
            if(res.state === "000000"){
                $('#customerName').val(res.data[0].customerName);
                return;
            }
            $('#customerNo').val("");
            $('#customerName').val("");
            toastr.warning(res.message);
        }
    })

};

function getRate(){
        toastr.options = messageOpts;
        var buyCurrency = $('#buyCurrency').val();

        var sellCurrency = $('#sellCurrency').val();

        if(buyCurrency != "" && sellCurrency != "" && sellCurrency == buyCurrency){
           return  toastr.warning("请输入有效的货币对!")
        }

    $.ajax({
        url: window.portAddress + "/rate/getRate",
        type: "get",
        data: {
            buyCurrency: buyCurrency,
            sellCurrency: sellCurrency
        },
        dataType: "json",
        success: function (res) {
            if(res.state === "000000"){
                $('#costRate').val(res.data);
                return;
            }
            $('#buyCurrency').val("");
            $('#sellCurrency').val("");
            toastr.warning(res.message);
        }
    })

}

function checkAcceptedBooKRate(){
    toastr.options = messageOpts;

    var costRate = $('#costRate').val();
    var acceptedBooKRate = $('#acceptedBooKRate').val();

    if(acceptedBooKRate == null || acceptedBooKRate == ""){
        toastr.warning("请输入预定汇率!");
        return;
    }

    //校验预定汇率是否超过预期
    $.ajax({
        url: window.portAddress + "/rate/checkRate",
        type: "get",
        data: {
            costRate: costRate,
            acceptedBooKRate: acceptedBooKRate
        },
        dataType: "json",
        success: function (res) {
            if(res.state === "000000"){
                return;
            }
            $('#acceptedBooKRate').val("");
            toastr.warning(res.message);
        }
    })
}

function populateSellAmount(){
    toastr.options = messageOpts;

    var acceptedBooKRate = $('#acceptedBooKRate').val();

    var amount;

    if($('#buyAmount').val() == null || $('#buyAmount').val() == ""){
        $('#sellAmount').val('');
        document.getElementById('sellAmount').disabled = false;
       return
    };

    if($('#buyAmount').val() != null || $('#buyAmount').val() != ""){
        amount = $('#buyAmount').val();
    };


    //计算另外一边的金额
    $.ajax({
        url: window.portAddress + "/contract/populateAmount",
        type: "post",
        data: {
            acceptedBooKRate: acceptedBooKRate,
            amount: amount,
            buyOrSell: "B"
        },
        dataType: "json",
        success: function (res) {
            if(res.state === "000000"){
                $('#sellAmount').val(res.data);
                document.getElementById('sellAmount').disabled = true;
                return;
            }
            toastr.warning(res.message);
        }
    })
}

function populateBuyAmount(){
    toastr.options = messageOpts;

    var acceptedBooKRate = $('#acceptedBooKRate').val();

    var amount;

    if($('#sellAmount').val() == null || $('#sellAmount').val() == ""){
        $('#buyAmount').val('');
        document.getElementById('buyAmount').disabled = false;
        return
    };

    if($('#sellAmount').val() != null || $('#sellAmount').val() != ""){
        amount = $('#sellAmount').val();
    };

    //计算另外一边的金额
    $.ajax({
        url: window.portAddress + "/contract/populateAmount",
        type: "post",
        data: {
            acceptedBooKRate: acceptedBooKRate,
            amount: amount,
            buyOrSell: "S"
        },
        dataType: "json",
        success: function (res) {
            if(res.state === "000000"){
                $('#buyAmount').val(res.data);
                document.getElementById('buyAmount').disabled = true;
                return;
            }
            toastr.warning(res.message);
        }
    })
}

function  sendContract() {

    // 创建一个新的Date对象，它将包含当前日期和时间
    var date = new Date();

    var year = date.getFullYear();

    var month = date.getMonth() + 1;

    var day = date.getDate();

    // 可以按照需要格式化日期和时间
    var CurrentDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);

    if($("#settlementDate").val() < CurrentDate){
        toastr.warning("输入日期不符合要求!");
        return;
    }

    $.ajax({
        url: window.portAddress + "/contract/createContract",
        type: "post",
        data: {
            customerNo: $("#customerNo").val(),
            customerName: $("#customerName").val(),
            buyCurrency: $("#buyCurrency").val(),
            sellCurrency: $("#sellCurrency").val(),
            costRate: $("#costRate").val(),
            acceptedBooKRate: $("#acceptedBooKRate").val(),
            buyAmount: $("#buyAmount").val(),
            sellAmount: $("#sellAmount").val(),
            settlementDate: $("#settlementDate").val(),
            userName: window.sessionStorage.getItem("userName")
        },
        dataType: "json",
        success: function (res) {
            if(res.state === "000000"){
                $("#customerNo").val("");
                $("#customerName").val("");
                $("#buyCurrency").val("");
                $("#sellCurrency").val("");
                $("#costRate").val("");
                $("#acceptedBooKRate").val("");
                $("#buyAmount").val("");
                $("#sellAmount").val("");
                $("#settlementDate").val("");
                toastr.success(res.message);
                return;
            }

            toastr.warning(res.message);
        }
    });

}






